package com.gc.business.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gc.common.support.Convert;
import com.gc.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.gc.business.mapper.UserElevatorAllotMapper;
import com.gc.business.domain.UserElevatorAllot;
import com.gc.business.service.IUserElevatorAllotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

/**
 * 员工电梯分配 服务层实现
 * 
 * @author hruiyan
 * @date 2020-11-17
 */
@Slf4j
@Service
public class UserElevatorAllotServiceImpl extends ServiceImpl<UserElevatorAllotMapper, UserElevatorAllot> implements IUserElevatorAllotService {

    @Override
    public List<UserElevatorAllot> list(UserElevatorAllot userElevatorAllot){
        LambdaQueryWrapper<UserElevatorAllot> wrapper = new LambdaQueryWrapper<>();
        if(userElevatorAllot != null){
            if(StringUtils.isNotEmpty(userElevatorAllot.getAllotId())){
                wrapper.eq(UserElevatorAllot::getAllotId, userElevatorAllot.getAllotId());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getUserId())){
                wrapper.eq(UserElevatorAllot::getUserId, userElevatorAllot.getUserId());
            }
            if(StringUtils.isNotNull(userElevatorAllot.getElevatorId())){
                wrapper.eq(UserElevatorAllot::getElevatorId, userElevatorAllot.getElevatorId());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getCreateBy())){
                wrapper.eq(UserElevatorAllot::getCreateBy, userElevatorAllot.getCreateBy());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getCreateTime())){
                wrapper.eq(UserElevatorAllot::getCreateTime, userElevatorAllot.getCreateTime());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getUpdateBy())){
                wrapper.eq(UserElevatorAllot::getUpdateBy, userElevatorAllot.getUpdateBy());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getUpdateTime())){
                wrapper.eq(UserElevatorAllot::getUpdateTime, userElevatorAllot.getUpdateTime());
            }
            if(StringUtils.isNotEmpty(userElevatorAllot.getRemark())){
                wrapper.eq(UserElevatorAllot::getRemark, userElevatorAllot.getRemark());
            }
        }
        return baseMapper.selectList(wrapper);
    }


    /**
     * 保存中间表
     * @param userId
     * @param elevators
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveElevatorAllot(Long userId,String elevators){
        baseMapper.deleteInfoByUserId(userId);
        UserElevatorAllot userElevatorAllot = null;
        Long[] elevatorArray = Convert.toLongArray(elevators);
        for (Long elevatorId : elevatorArray) {
            userElevatorAllot = new UserElevatorAllot();
            userElevatorAllot.setElevatorId(elevatorId);
            userElevatorAllot.setUserId(userId);
            baseMapper.insert(userElevatorAllot);
        }
        return 0;
    }

}
